package com.example.blogback.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.blogback.dto.ArticleDTO;
import com.example.blogback.entity.Article;
import com.example.blogback.entity.ArticleContent;
import com.example.blogback.mapper.ArticleContentMapper;
import com.example.blogback.mapper.ArticleMapper;
import com.example.blogback.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {

    @Resource
    private ArticleContentMapper articleContentMapper;

    @Override
    @Transactional
    public void createArticle(ArticleDTO articleDTO) {
        // 保存文章基本信息
        Article article = new Article();
        BeanUtils.copyProperties(articleDTO, article);
        save(article);

        // 保存文章内容
        ArticleContent articleContent = new ArticleContent();
        articleContent.setArticleId(article.getId());
        articleContent.setContent(articleDTO.getContent());
        articleContentMapper.insert(articleContent);
    }

    @Override
    @Transactional
    public void updateArticle(ArticleDTO articleDTO) {
        // 更新文章基本信息
        Article article = getById(articleDTO.getId());
        if (article == null) {
            throw new RuntimeException("文章不存在");
        }
        BeanUtils.copyProperties(articleDTO, article);
        updateById(article);

        // 更新文章内容
        ArticleContent articleContent = articleContentMapper.selectOne(
                new LambdaQueryWrapper<ArticleContent>().eq(ArticleContent::getArticleId, article.getId()));
        articleContent.setContent(articleDTO.getContent());
        articleContentMapper.updateById(articleContent);
    }

    @Override
    @Transactional
    public void deleteArticle(Long id) {
        removeById(id);
    }

    @Override
    public void setFeatured(Long id, Boolean featured) {
        Article article = getById(id);
        if (article == null) {
            throw new RuntimeException("文章不存在");
        }
        article.setFeatured(featured);
        updateById(article);
    }

    @Override
    public ArticleDTO getArticleDetail(Long id) {
        // 获取文章基本信息
        Article article = getById(id);
        if (article == null) {
            throw new RuntimeException("文章不存在");
        }

        // 获取文章内容
        ArticleContent articleContent = articleContentMapper.selectOne(
                new LambdaQueryWrapper<ArticleContent>().eq(ArticleContent::getArticleId, id));

        // 转换为DTO
        ArticleDTO articleDTO = new ArticleDTO();
        BeanUtils.copyProperties(article, articleDTO);
        articleDTO.setContent(articleContent.getContent());

        return articleDTO;
    }

    @Override
    public IPage<Article> getArticleList(Long categoryId, Integer page, Integer size) {
        LambdaQueryWrapper<Article> wrapper = new LambdaQueryWrapper<Article>()
                .eq(categoryId != null, Article::getCategoryId, categoryId)
                .orderByDesc(Article::getCreateTime);
        return page(new Page<>(page, size), wrapper);
    }

    @Override
    public void incrementViewCount(Long id) {
        Article article = getById(id);
        if (article == null) {
            throw new RuntimeException("文章不存在");
        }
        article.setViewCount(article.getViewCount() + 1);
        updateById(article);
    }
}